package com.ocom.company.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.google.common.collect.Lists;
import com.ocom.common.constant.CompanyServiceUrl;
import com.ocom.common.entity.access.EntityMjRegionInfo;
import com.ocom.common.entity.card.EntityXfMerchantInfo;
import com.ocom.common.entity.company.EntityRecentAdminAccount;
import com.ocom.common.entity.human.EntityDeptInfo;
import com.ocom.common.entity.typehandler.MngUserHandle;
import com.ocom.common.request.company.GetLoginCompanyListRequest;
import com.ocom.common.request.company.RefreshRecentCompanyRequest;
import com.ocom.common.request.human.GetDeptByIdsRequest;
import com.ocom.common.request.human.GetMjRegionByIdsRequest;
import com.ocom.common.request.payconfig.GetMerchInfoByIdsRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.company.GetLoginCompanyListVo;
import com.ocom.company.feign.AccessClient;
import com.ocom.company.feign.HumanClient;
import com.ocom.company.feign.PayConfigClient;
import com.ocom.company.service.RecentAdminAccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@Slf4j
public class RecentAdminAccountController {

    @Autowired
    private HumanClient humanClient;

    @Autowired
    private AccessClient accessClient;

    @Autowired
    private PayConfigClient payConfigClient;

    @Autowired
    private RecentAdminAccountService recentAdminAccountService;

    @PostMapping(CompanyServiceUrl.URL_refreshRecentCompany)
    public Result refreshRecentCompany(@RequestBody RefreshRecentCompanyRequest request){
        EntityRecentAdminAccount entityRecentAdminAccount = Convert.convert(EntityRecentAdminAccount.class,request);
        entityRecentAdminAccount.setComId(request.getCompanyId());
        return ResultUtil.success(this.recentAdminAccountService.saveOrUpdate(request.getCompanyId(),entityRecentAdminAccount));
    }

    @PostMapping(CompanyServiceUrl.URL_getLoginCompanyList)
    public Result getLoginCompanyList(@RequestBody GetLoginCompanyListRequest request){
        List<GetLoginCompanyListVo> getLoginCompanyList = new ArrayList<>();
        if(ObjectUtil.isEmpty(request.getDeptId())){request.setDeptId(0L);}
        if(ObjectUtil.isEmpty(request.getRole())){request.setRole(0L);}
        if(ObjectUtil.isEmpty(request.getRegionId())){request.setRegionId(0L);}
        if(ObjectUtil.isEmpty(request.getMerchId())){request.setMerchId(0L);}
        Map<Long, EntityDeptInfo> deptInfoMap = new HashMap<>();
        Map<Long, EntityMjRegionInfo> regionInfoMap = new HashMap<>();
        Map<Integer, EntityXfMerchantInfo> xfMerchantInfoMap = new HashMap<>();

        if(Lists.newArrayList(0).contains(request.getQueryMode())){
            getLoginCompanyList = this.recentAdminAccountService.selectAdminLoginList(request);
        }else if(Lists.newArrayList(1,2).contains(request.getQueryMode())){
            getLoginCompanyList = this.recentAdminAccountService.selectUserLoginList(request.getComId(), request);
            //获取部门
            Set<Long> deptIds = new HashSet<>();
            Set<Long> regionIds = new HashSet<>();
            Set<Integer> merchIds = new HashSet<>();

            for (GetLoginCompanyListVo getLoginCompanyListVo:getLoginCompanyList){
                String phone = getLoginCompanyListVo.getPhone();
                try {
                    if(!NumberUtil.isNumber(phone)){
                        MngUserHandle mngUserHandle = new MngUserHandle();
                        phone = mngUserHandle.decrypt(phone);
                    }
                    getLoginCompanyListVo.setPhone(phone);
                }catch (Exception ignore){

                }
                if(getLoginCompanyListVo.getDeptId() > 0){
                    deptIds.add(getLoginCompanyListVo.getDeptId());
                }

                if(getLoginCompanyListVo.getRegionId() > 0){
                    regionIds.add(getLoginCompanyListVo.getRegionId());
                }

                if(getLoginCompanyListVo.getMerchId() > 0){
                    merchIds.add(getLoginCompanyListVo.getMerchId());
                }

            }

            if(ObjectUtil.isNotEmpty(deptIds)){
                GetDeptByIdsRequest getDeptByIdsRequest = new GetDeptByIdsRequest();
                getDeptByIdsRequest.setComId(request.getComId());
                getDeptByIdsRequest.setIds(Lists.newArrayList(deptIds));
                Result<List<EntityDeptInfo>> resultDept = this.humanClient.getDeptByIds(getDeptByIdsRequest);
                if(ObjectUtil.isNotEmpty(resultDept.getData())){
                    for (EntityDeptInfo entityDeptInfo:resultDept.getData()){
                        deptInfoMap.put(entityDeptInfo.getDeptId(),entityDeptInfo);
                    }
                }
            }

            if(ObjectUtil.isNotEmpty(regionIds)){
                GetMjRegionByIdsRequest getMjRegionByIdsRequest = new GetMjRegionByIdsRequest();
                getMjRegionByIdsRequest.setComId(request.getComId());
                getMjRegionByIdsRequest.setIds(Lists.newArrayList(regionIds));
                Result<List<EntityMjRegionInfo>> resultMjRegion =  this.accessClient.getMjRegionByIds(getMjRegionByIdsRequest);
                if(ObjectUtil.isNotEmpty(resultMjRegion.getData())){
                    for (EntityMjRegionInfo entityMjRegionInfo:resultMjRegion.getData()){
                        regionInfoMap.put(entityMjRegionInfo.getDeptId(),entityMjRegionInfo);
                    }
                }
            }

            if(ObjectUtil.isNotEmpty(merchIds)){
                GetMerchInfoByIdsRequest getMjRegionByIdsRequest = new GetMerchInfoByIdsRequest();
                getMjRegionByIdsRequest.setComId(request.getComId());
                getMjRegionByIdsRequest.setIds(Lists.newArrayList(merchIds));
                Result<List<EntityXfMerchantInfo>> resultMjRegionInfo =  this.payConfigClient.GetMerchInfoByIds(getMjRegionByIdsRequest);
                if(ObjectUtil.isNotEmpty(resultMjRegionInfo.getData())){
                    for (EntityXfMerchantInfo entityXfMerchantInfo:resultMjRegionInfo.getData()){
                        xfMerchantInfoMap.put(entityXfMerchantInfo.getMerchId(),entityXfMerchantInfo);
                    }
                }
            }


        }



        for (GetLoginCompanyListVo getLoginCompanyListVo:getLoginCompanyList){
            if(ObjectUtil.isEmpty(getLoginCompanyListVo.getRoleName()) && getLoginCompanyListVo.getRole() < 10000){
                getLoginCompanyListVo.setRoleName(this.recentAdminAccountService.getRoleName(getLoginCompanyListVo.getRole()));
            }
            getLoginCompanyListVo.setDeptName("");

            //部门
            if(getLoginCompanyListVo.getDeptId() > 0){
                if(ObjectUtil.isNotEmpty(deptInfoMap.get(getLoginCompanyListVo.getDeptId()))){
                    getLoginCompanyListVo.setDeptName(getLoginCompanyListVo.getDeptName()+deptInfoMap.get(getLoginCompanyListVo.getDeptId()).getDeptName());
                }else{
                    EntityDeptInfo entityDeptInfo = new EntityDeptInfo();
                    entityDeptInfo.setComId(getLoginCompanyListVo.getCompanyId());
                    entityDeptInfo.setDeptId(getLoginCompanyListVo.getDeptId());
                    Result<EntityDeptInfo> resultA = this.humanClient.getDeptById(entityDeptInfo);
                    if(ObjectUtil.isNotEmpty(resultA.getData())){
                        getLoginCompanyListVo.setDeptName(getLoginCompanyListVo.getDeptName()+resultA.getData().getDeptName());
                        deptInfoMap.put(resultA.getData().getDeptId(),resultA.getData());
                    }
                }

            }

            //片区
            if(getLoginCompanyListVo.getRegionId() > 0){
                if(ObjectUtil.isNotEmpty(regionInfoMap.get(getLoginCompanyListVo.getRegionId()))){
                    getLoginCompanyListVo.setDeptName(getLoginCompanyListVo.getDeptName()+regionInfoMap.get(getLoginCompanyListVo.getRegionId()).getRegionName());
                }else{
                    EntityMjRegionInfo entityMjRegionInfo = new EntityMjRegionInfo();
                    entityMjRegionInfo.setRegionId(getLoginCompanyListVo.getRegionId());
                    entityMjRegionInfo.setComId(getLoginCompanyListVo.getCompanyId());
                    Result<EntityMjRegionInfo> resultB  = this.accessClient.getMjRegionInfo(entityMjRegionInfo);
                    if(ObjectUtil.isNotEmpty(resultB.getData())){
                        getLoginCompanyListVo.setDeptName(getLoginCompanyListVo.getDeptName()+resultB.getData().getRegionName());
                        regionInfoMap.put(resultB.getData().getRegionId(),resultB.getData());
                    }
                }

            }


            if(getLoginCompanyListVo.getMerchId() > 0){
                if(ObjectUtil.isNotEmpty(xfMerchantInfoMap.get(getLoginCompanyListVo.getMerchId()))){
                    getLoginCompanyListVo.setDeptName(getLoginCompanyListVo.getDeptName()+xfMerchantInfoMap.get(getLoginCompanyListVo.getMerchId()).getMerchName());
                }else{
                    Result<EntityXfMerchantInfo> resultC = payConfigClient.GetMerchInfo(getLoginCompanyListVo.getCompanyId(),getLoginCompanyListVo.getMerchId());
                    if(ObjectUtil.isNotEmpty(resultC.getData())){
                        getLoginCompanyListVo.setDeptName(getLoginCompanyListVo.getDeptName()+resultC.getData().getMerchName());
                        xfMerchantInfoMap.put(resultC.getData().getMerchId(),resultC.getData());
                    }
                }

            }

        }
//        return getLoginCompanyList;
        return ResultUtil.success(getLoginCompanyList);
    }

}
